package cn.iocoder.yudao.module.bpm.service.taginfo;

import cn.iocoder.yudao.module.bpm.dal.dataobject.taggroupinfo.BpmTagGroupInfoDO;
import cn.iocoder.yudao.module.bpm.service.taggroupinfo.BpmTagGroupInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.bpm.controller.admin.taginfo.vo.*;
import cn.iocoder.yudao.module.bpm.dal.dataobject.taginfo.BpmTagInfoDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.bpm.dal.mysql.taginfo.BpmTagInfoMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.bpm.enums.ErrorCodeConstants.*;

/**
 * 标签信息 Service 实现类
 *
 * @author 韩佳乐
 */
@Service
@Validated
public class BpmTagInfoServiceImpl implements BpmTagInfoService {

    @Resource
    private BpmTagInfoMapper tagInfoMapper;
    @Resource
    private BpmTagGroupInfoService bpmTagGroupInfoService;
    @Override
    public Long createTagInfo(BpmTagInfoSaveReqVO createReqVO) {
        // 插入
        BpmTagInfoDO tagInfo = BeanUtils.toBean(createReqVO, BpmTagInfoDO.class);
        tagInfoMapper.insert(tagInfo);
        // 返回
        return tagInfo.getId();
    }

    @Override
    public void updateTagInfo(BpmTagInfoSaveReqVO updateReqVO) {
        // 校验存在
        validateTagInfoExists(updateReqVO.getId());
        // 更新
        BpmTagInfoDO updateObj = BeanUtils.toBean(updateReqVO, BpmTagInfoDO.class);
        tagInfoMapper.updateById(updateObj);
    }

    @Override
    public void deleteTagInfo(Long id) {
        // 校验存在
        validateTagInfoExists(id);
        // 删除
        tagInfoMapper.deleteById(id);
    }

    private void validateTagInfoExists(Long id) {
        if (tagInfoMapper.selectById(id) == null) {
            throw exception(TAG_INFO_NOT_EXISTS);
        }
    }

    @Override
    public BpmTagInfoDO getTagInfo(Long id) {
        return tagInfoMapper.selectById(id);
    }

    @Override
    public PageResult<BpmTagInfoDO> getTagInfoPage(BpmTagInfoPageReqVO pageReqVO) {
        return tagInfoMapper.selectPage(pageReqVO);
    }

    @Override
    public List<BpmTagInfoDO> getTagInfoByGroupId(Long groupId) {
        BpmTagGroupInfoDO tagGroupInfo = bpmTagGroupInfoService.getTagGroupInfo(groupId);
        if (tagGroupInfo == null){
            throw exception(TAG_GROUP_INFO_NOT_EXISTS);
        }
        QueryWrapper<BpmTagInfoDO> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id",groupId);
        return tagInfoMapper.selectList(wrapper);
    }

}